package com.wise.site.widget;

import java.util.Collections;

import com.wise.site.adapter.InfoListAdatper;
import com.wise.site.adapter.InfoListAdatper.ITEM_TYPE;
import com.wise.site.detail.InfoDetailsActivity;
import com.wise.site.main.IndexerActivity;
import com.wise.site.protocol.result.IndexerBarEntry;
import com.wise.site.protocol.result.InfoItem;
import com.wise.site.utils.CollectorUtils;
import com.wise.site.utils.CommentUtils;
import com.wise.site.utils.Constanct;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ListView;
import android.widget.TextView;

public class IndexerBarGallery extends Gallery implements OnItemSelectedListener , OnItemClickListener{
	private IndexBarItemView mSelectedItem;
	private boolean isFrist = true;
	private boolean isToIndex = false;
	
	private View headline;
	private ListView other;
	private InfoListAdatper mInfoListAdatper;
	private int tabId;
	
	public IndexerBarGallery(Context context) {
		this(context, null);
	}

	public IndexerBarGallery(Context context, AttributeSet attrs) {
		this(context, attrs, -1);
	}
	
	public IndexerBarGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setOnItemSelectedListener(this);
		setOnItemClickListener(this);
		setUnselectedAlpha(1.1f);
		
		mInfoListAdatper = new InfoListAdatper(ITEM_TYPE.INFO_TYPE, context);
//		mOtherAction = new InfoListAction(ACTION_TYPE.ACTION_COMMON, "getmessinfo");
//		mOtherAction.setActionListener(new ActionListener<InfoItemsResult>() {
//
//			@Override
//			public void onSucceed(InfoItemsResult result) {
//				mInfoListAdatper.setList(result.list);
//				if(mProgressDialog != null){
//					mProgressDialog.dismiss();
//				}
//			}
//
//			@Override
//			public void onError(int resultCode) {
//				
//			}
//		});
	}
	
//	public void setFrist(boolean isFrist) {
//		this.isFrist = isFrist;
//	}

	public void setTabId(int tabId) {
		this.tabId = tabId;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		if(view == null){
			return;
		}
		
		if(isToIndex){
			isToIndex = false;
			switchView(view);
			return;
		}
		
		if(isFrist){
			isFrist = false;
			mSelectedItem = (IndexBarItemView) view;
			setSelector(view);
		}
		
		if(view instanceof IndexBarItemView){
			if(((IndexBarItemView) view).isHeadline()){
				((IndexBarItemView) view).setTextColor(0xffc3ff4e);
			} else {
				((IndexBarItemView) view).setTextColor(0xffffffff);
			}
		}
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int keyCode;
		if (e2.getX() > e1.getX()) {
			keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(keyCode, null);
		return true;
	}

	public void setSelector(View view) {
			if(mSelectedItem != null){
				mSelectedItem.setSelect(false);
			}
			
			((IndexBarItemView) view).setSelect(true);
			mSelectedItem = (IndexBarItemView) view;
			if(mSelectedItem.isHeadline()){
				mSelectedItem.setTextColor(0xffc3ff4e);
			} else {
				mSelectedItem.setTextColor(0xffffffff);
			}
			
			IndexerBarEntry entry = (IndexerBarEntry) view.getTag();
			CommentUtils.getInstance().uploadCount(tabId, entry.id, entry.text);
	}
	
	public void setHeadline(View headline) {
		this.headline = headline;
	}

	public void setOther(ListView other) {
		this.other = other;
		other.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int index, long id) {
				InfoItem item = (InfoItem) v.getTag();
				Intent intent = new Intent(getContext().getApplicationContext(), InfoDetailsActivity.class);
				intent.putExtra(Constanct.INFO_ENTRY, item);
				getContext().startActivity(intent);
			}
		});
		mInfoListAdatper.setListView(other);
	}
	
	public IndexerBarEntry getSelectEntry(){
		return (IndexerBarEntry) mSelectedItem.getTag();
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		switchView(view);
	}
	

	/**
	 * 切到指定的位置
	 *
	 * huangyue  2012-10-3
	 */
	public void toIndex(int index){
		int pos = getSelectedItemPosition();
		isToIndex = true;
		setSelection(pos + index);
	}

	private void switchView(View view) {
		if(view instanceof IndexBarItemView){
			setSelector(view);
			IndexerBarEntry entry = (IndexerBarEntry) view.getTag();
			if(headline == null || other == null){
				return;
			}
			if(entry.type == Constanct.HEADLIINE_ID){
				headline.setVisibility(VISIBLE);
				other.setVisibility(GONE);
				((IndexBarItemView)view).setTextColor(0xffc3ff4e);
				((IndexerActivity)getContext()).loadHeadlineDate();
			} else {
				headline.setVisibility(GONE);
				other.setVisibility(VISIBLE);
				mInfoListAdatper.setTypeId(entry.id);
				mInfoListAdatper.moveToFristPage();
				mInfoListAdatper.loadData();
			}
		}
	}
}
